<template>
  <div>
    <!-- 搜索表单 -->
    <div style="text-align: center">
      <el-input style="width: 240px;" placeholder="请输入租借单号" v-model="params.hireNo"></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入用户名称" v-model="params.userName" ></el-input>
      <el-input style="width: 240px; margin-left: 5px" placeholder="请输入单车名称" v-model="params.bikeName"></el-input>
      <el-button style="margin-left: 5px" type="primary" @click="load"><i class="el-icon-search"
                                                                          style="margin-right: 5px"></i>搜索
      </el-button>
      <el-button style="margin-left: 5px" type="warning" @click="reset"><i class="el-icon-refresh"
                                                                           style="margin-right: 5px"></i>重置
      </el-button>
    </div>
    <!-- 用户详细数据 -->
    <el-table :data="tableData" row-key="id" default-expand-all :cell-style="{'text-align':'center'}" :header-cell-style="{'text-align':'center'}">
      <el-table-column prop="id" label="编号"></el-table-column>
      <el-table-column prop="hireNo" label="租借单号"></el-table-column>
      <el-table-column prop="userName" label="租借用户"></el-table-column>
      <el-table-column prop="bikeName" label="租借单车"></el-table-column>
      <el-table-column prop="start" label="租借开始时间" width="200px"></el-table-column>
      <el-table-column prop="end" label="租借结束时间" width="200px"></el-table-column>
      <el-table-column prop="status" label="租借状态">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.status === 1">租借中</el-tag>
          <el-tag type="primary" v-if="scope.row.status === 0">已归还</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="note" label="超时提醒">
        <template v-slot="scope">
          <el-tag type="success" v-if="scope.row.note === '未超时'">{{scope.row.note}}</el-tag>
          <el-tag type="danger" v-else>{{scope.row.note}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="money" label="租金"></el-table-column>

      <el-table-column label="操作">
        <template v-slot="scope">
          <!-- scope.row就是当前行数据 -->
<!--          <el-button type="primary" @click="$router.push('/editHire?id=' + scope.row.id )">编辑</el-button>-->
          <!-- 确认删除 -->
          <el-popconfirm
              title="您确定要结束该订单吗？"
              style="margin-left: 10px"
              @confirm="end(scope.row.id)">
            <el-button slot="reference" type="danger" v-if="scope.row.status == 1">强制还车</el-button>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div>
      <el-pagination style="margin-top: 10px; text-align: center" background :total="total" :page-size="params.pageSize"
                     :current-page="params.pageNum" layout="prev,pager,next" @current-change="handleCurrentChange">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import request from "@/utils/request";
import Cookies from "js-cookie";

export default {
  account: 'Hire',
  data() {
    return {
      admin: Cookies.get('admin') ? JSON.parse(Cookies.get('admin')) : {},
      tableData: [],
      total: 0,
      params: {
        pageNum: 1,
        pageSize: 7,
        hireNo: '',
        userName: '',
        bikeName: ''
      },
    }
  },
  created() {
    this.load();
  },
  methods: {
    load() {
      request.get("/hire/page", { params: this.params}).then(res => {
        if (res.code === '200') {
          console.log(res)
          this.tableData = res.data.list
          this.total = res.data.total
        }
      })
    },
    reset() {
      this.params = {
        pageNum: 1,
        pageSize: 7,
        hireNo: '',
        userName: '',
        bikeName: ''
      }
    },
    handleCurrentChange(pageNum) {
        this.params.pageNum = pageNum;
        this.load();
      },
    end(id) {
        request.put("/hire/",{id}).then(res => {
          if (res.code === '200') {
            this.$notify.success('锁车成功');
          this.load();
        } else {
          this.$notify.error(res.msg);
        }
      })
    },
  }
}
</script>

<style scoped>


</style>